package com.mystore.manage.controller;

import com.mystore.common.jsonx.JSONEntity;
import com.mystore.common.utils.file.FileUpDownUtils;
import com.mystore.framework.pojo.FileModel;
import com.mystore.framework.service.impl.IFileModelServiceImpl;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 文件模型表(FileModel)表控制层
 */
@Controller
@RequestMapping("manage/fileModel")
public class FileModelController {
    /**
     * 服务对象
     */
    @Resource
    private IFileModelServiceImpl iFileModelServiceImpl;

    @PostMapping("/upload.do")
    @ResponseBody
    public JSONEntity upload(MultipartFile[] file, HttpServletRequest request){
        List<String> path;
        path=FileUpDownUtils.upload(file,request);
        if (path.isEmpty())
            return JSONEntity.error(null);
        List<Integer> ids=new ArrayList<>();
        AtomicBoolean ok=new AtomicBoolean(true);
        path.forEach(p->{FileModel fileModel=new FileModel();
            fileModel.setFileModelPath(p);
            int id=0;
            if((id=iFileModelServiceImpl.insertOne(fileModel))>0)
                ids.add(id);
            else
                ok.set(false);
        });
        if(ok.get())
            return JSONEntity.ok(ids.toArray());
        else
            return JSONEntity.error(null);
    }
    @GetMapping("/page.do")
    public String page(@RequestParam(defaultValue="") String data,Model model, @RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size,String result) {
        IPage<FileModel> fileModelPage = iFileModelServiceImpl.selectPageByData(data,new Page<>(current, size));
        model.addAttribute("fileModels", fileModelPage);
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        return "fileModel_page";
    }

    @GetMapping("/insert.do")
    public String insert(Model model) {
        model.addAttribute("mode", "insert");
        return "fileModel_info";
    }

    @GetMapping("/update.do")
    public String update(Model model, int fileModelId) {
        model.addAttribute("fileModel", iFileModelServiceImpl.selectById(fileModelId));
        model.addAttribute("mode", "update");
        return "fileModel_info";
    }

    @PostMapping("/save.do")
    public String save(RedirectAttributes model, FileModel fileModel, String mode) {
        if (mode.equals("insert"))
            model.addAttribute("result", iFileModelServiceImpl.insertOne(fileModel)>0);
        else
            model.addAttribute("result", iFileModelServiceImpl.updateOne(fileModel));
        model.addAttribute("mode", mode);
        return "redirect:/manage/fileModel/page.do";
    }

    @GetMapping("/delete.do")
    public String delete(RedirectAttributes model, int fileModelId) {
        model.addAttribute("result", iFileModelServiceImpl.deleteById(fileModelId));
        return "redirect:/manage/fileModel/page.do";
    }

}

